import Shapes.Line;
import Shapes.Oval;
import Shapes.Rectangle;
import Shapes.Shape;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-12-16
 * Time: 14:13
 */


/**
 * 鼠标监听事件
 */
public class Listener  implements MouseListener,MouseMotionListener,ActionListener {

    //记录两个点的坐标,鼠标按下的坐标和释放的坐标
    //从而确定图形的位置
    public int x1;
    public int x2;
    public int y1;
    public int y2;

    //画笔的颜色,在按钮的监听事件中会修改
    public Color color=Color.black;

    //画笔的粗细,在按钮的监听事件中会修改
    public int size=1;

    //记录要画的形状,默认是曲线
    public String type="Curve";
    public Graphics2D graphics;//画笔

    //获取当前那个画板,从而能够在鼠标拖拽时,repaint
    public JPanel jPanel;

    //存储所有已经画好的图形
    public ArrayList<Shape> arrayList=new ArrayList<>();

    /**
     * 设置画笔,得到画板的画笔
     * @param graphics
     */
    public void setGraphics(Graphics graphics){
        this.graphics=(Graphics2D)graphics;
    }

    /**
     * 设置数组,这个数组是画板所有,因为画板要repaint,所以画板要存储所有已经画过的图形
     * 在这个鼠标监听事件里面会进行绘画,所以要将新画出来的图形添加到数组中
     * @param arrayList
     */
    public void setArrayList(ArrayList<Shape> arrayList){
        this.arrayList=arrayList;
    }

    /**
     * 得到画板
     * @param jPanel
     */

    public void setPanel(JPanel jPanel){
        this.jPanel=jPanel;
    }

    /**
     * 鼠标按压事件
     * @param e
     */
    @Override
    public void mousePressed(MouseEvent e) {
        //得到起始坐标
        x1=e.getX();
        y1=e.getY();
        //设置画笔
        this.graphics.setColor(color);
        this.graphics.setStroke(new BasicStroke(this.size));
    }

    /**
     * 鼠标释放事件
     * @param e
     */

    @Override
    public void mouseReleased(MouseEvent e) {
        //得到结束位置
        x2=e.getX();
        y2=e.getY();
        //根据对应的图形进行绘画
        switch (this.type){
            case "Line":
                Line line=new Line(x1,y1,x2,y2,color,size);
                graphics.setStroke(new BasicStroke(this.size));
                line.draw(graphics);
                //图形添加到数组中
                arrayList.add(line);
                break;
            case "Rect":
                Rectangle rectangle =new Rectangle(x1,y1,x2,y2,color,size);
                graphics.setStroke(new BasicStroke(this.size));
                rectangle.draw(graphics);
                //图形添加到数组中
                arrayList.add(rectangle);
                break;
            case "Oval":
                Oval oval =new Oval(x1,y1,x2,y2,color,size);
                graphics.setStroke(new BasicStroke(this.size));
                oval.draw(graphics);
                //图形添加到数组中
                arrayList.add(oval);
                break;
        }
    }

    /**
     * 鼠标拖动事件
     * @param e
     */
    @Override
    public void mouseDragged(MouseEvent e) {
        //得到位置
        x2=e.getX();
        y2=e.getY();
        //进行重绘
        jPanel.repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }


    @Override
    public void mouseClicked(MouseEvent e) {}
    @Override
    public void mouseEntered(MouseEvent e) {}
    @Override
    public void mouseExited(MouseEvent e) {}


    /**
     * 按钮点击事件
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if(!e.getActionCommand().equals("")){
            //按钮上有内容,判断是哪个按钮被点击了
            String str=e.getActionCommand();
            //如果是选择了线的粗细,就相应的修改线条粗细
            if(str.equals("小号线")){
                this.size=2;
            }else if(str.equals("中号线")){
                this.size=5;
            }else if(str.equals("大号线")){
                this.size=9;
            }else{
                //如果是选择了不同的图形,就修改形状
                this.type=str;
            }
        }else{
            //按钮上没有内容,说明选的是颜色,就更改当前的颜色
            JButton jButton=(JButton) e.getSource();
            this.color=jButton.getBackground();
        }
    }
}
